package br.com.fsa.aplication.Database;

import java.util.HashMap;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import br.com.fsa.aplication.Exception.ConectionException;
import br.com.fsa.aplication.Model.bean.NotaFiscal;
import br.com.fsa.aplication.Util.HibernateUtil;

public class NotaFiscalDAO extends DaoGenericoImpl<NotaFiscal> {
	
	public List obterFiltro(HashMap dto) {
		Query sql = null;
		String instrucao="";
		HashMap parametros = (HashMap) dto.get("parametros");
		Session session = null;
		Transaction t = null;
		try {
			session = HibernateUtil.getSession().openSession();
			t = session.beginTransaction();
			if (dto.get("tipo").equals("filtro")) {
				String alvo = (String) dto.get("alvo");
				instrucao = "select distinct ";
				instrucao += "nf." + alvo +" from NotaFiscal as nf where 1=1 ";

				if (parametros != null) {
					if (parametros.get("logistica") != null)
						instrucao += "and "+ "nf.logistica="
								+ parametros.get("logistica");
					if (parametros.get("estado") != null)
						instrucao += " and "+ "nf.estado="
								+"'"+ parametros.get("estado")+"'";
					if (parametros.get("cidade") != null)
						instrucao += " and "+ "nf.cidade="
								+"'"+ parametros.get("cidade")+"'";
					if (parametros.get("cliente") != null)
						instrucao += " and "+ "nf.cliente"
								+ parametros.get("destino");
					instrucao+=" and (nf.status is null or nf.status='Pendente')";
				}
			}
			
			sql = HibernateUtil.getSession().openSession()
					.createQuery(instrucao);

		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ConectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			t.commit();
			session.close();
			return sql.list();
		}
	}
	
	public List<NotaFiscal> obterLista(HashMap dto){
		Query sql = null;
		String instrucao="";
		HashMap parametros = (HashMap) dto.get("parametros");
		Session session=null;
		Transaction t=null;
		try {
			session = HibernateUtil.getSession().openSession();
			t = session.beginTransaction();
			if(dto.get("tipo").equals("lista")){
				instrucao="select nf from NotaFiscal as nf where 1=1 ";
				if (parametros != null) {
					if (parametros.get("logistica") != null)
						instrucao += "and "+ "nf.logistica="
								+ parametros.get("logistica");
					if (parametros.get("estado") != null)
						instrucao += " and "+ "nf.estado="
								+"'"+ parametros.get("estado")+"'";
					if (parametros.get("cidade") != null)
						instrucao += " and "+ "nf.cidade="
								+"'"+ parametros.get("cidade")+"'";
					if (parametros.get("cliente") != null)
						instrucao += " and "+ "nf.cliente="
								+ parametros.get("destino");
					instrucao+=" and (nf.status is null or nf.status='Pendente')";
				}
			}
			
			sql = HibernateUtil.getSession().openSession()
					.createQuery(instrucao);

		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ConectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			t.commit();
			session.close();
			return sql.list();
		}

		
	}

}
